package com.apinji.unionpay;

import com.apinji.common.domain.wrapper.Reply;
import com.apinji.common.plugin.Mutual;
import com.apinji.common.plugin.StrUtils;
import com.apinji.unionpay.dto.ConsumeDto;
import com.apinji.unionpay.dto.ConsumeQueryDto;
import com.apinji.unionpay.dto.ConsumeSMSBackTokenDto;
import com.apinji.unionpay.sdk.LogUtil;
import com.apinji.unionpay.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 轩辕夏夏 on 2017/7/4.
 *
 * 银联支付
 */
@RestController
@EnableAutoConfiguration
@EnableAsync
@RequestMapping("/v1.0/pay")
public class PayController {

    @Autowired
    private PayService payService;


    /**
     * 银联支付 获取短信验证码
     *
     * @param consumeSMSBackTokenDto
     * @return
     */
    @RequestMapping(value = "/consumeSMSBackToken", method = RequestMethod.POST)
    //public Reply consumeSMSBackToken(@RequestParam String userUUID, @RequestParam String orderId, @RequestParam String cardNo, @RequestParam String phoneNo, @RequestParam int txnAmt){
    public Reply consumeSMSBackToken(@RequestBody ConsumeSMSBackTokenDto consumeSMSBackTokenDto){
        if(StrUtils.isEmpty(consumeSMSBackTokenDto.getUserUUID())) {
            return Mutual.invalidParamAppend("userUUID");
        }
        if(StrUtils.isEmpty(consumeSMSBackTokenDto.getCardNo())) {
            return Mutual.invalidParamAppend("cardNo");
        }
        if(StrUtils.isEmpty(consumeSMSBackTokenDto.getOrderId())) {
            return Mutual.invalidParamAppend("orderId");
        }
        if(StrUtils.isEmpty(consumeSMSBackTokenDto.getOutOrderId())) {
            return Mutual.invalidParamAppend("outOrderId");
        }
        if(StrUtils.isEmpty(consumeSMSBackTokenDto.getPhoneNo())) {
            return Mutual.invalidParamAppend("phoneNo");
        }
        if(consumeSMSBackTokenDto.getTxnAmt() <= 0) {
            return Mutual.invalidParam("金额错误");
        }
        if(!Pattern.matches("^[0-9a-zA-Z]{4,40}", consumeSMSBackTokenDto.getOutOrderId())) {
            return Mutual.invalidParam("[outOrderId]支付账单流水ID错误");
        }
        //
        return payService.consumeSMSBackToken(consumeSMSBackTokenDto);
    }


    /**
     * 银联支付 token无跳转支付接口
     *
     * @param consumeDto
     * @return
     */
    @RequestMapping(value = "/consume")
//    public Reply consume(HttpServletRequest request, @RequestParam String userUUID, @RequestParam String orderId, @RequestParam String outOrderId, @RequestParam int txnAmt,
//                         @RequestParam String cardNo, @RequestParam String smsCode, @RequestParam(required = false)  String frontUrl){
    public Reply consume(HttpServletRequest request, @RequestBody ConsumeDto consumeDto){
        if(StrUtils.isEmpty(consumeDto.getUserUUID())) {
            return Mutual.invalidParam("无效参数[userUUID]");
        }
        if(StrUtils.isEmpty(consumeDto.getOrderId())) {
            return Mutual.invalidParam("无效参数[orderId]");
        }
        if(StrUtils.isEmpty(consumeDto.getOutOrderId())) {
            return Mutual.invalidParam("无效参数[outOrderId]");
        }
        if(consumeDto.getTxnAmt() <= 0) {
            return Mutual.invalidParam("支付金额不能少于0");
        }
        if(StrUtils.isEmpty(consumeDto.getOrderName())) {
            return Mutual.invalidParam("无效参数[orderName]");
        }
        if(!Pattern.matches("^[0-9a-zA-Z]{4,40}", consumeDto.getOutOrderId())) {
            return Mutual.invalidParam("[outOrderId]支付账单流水ID错误");
        }
        return payService.consume(request, consumeDto);
    }


    /**
     * 交易状态查询
     * @param consumeQueryDto
     * @return
     */
    @RequestMapping(value = "/consumeQuery")
    public Reply consumeQuery(@RequestBody ConsumeQueryDto consumeQueryDto){
        if(StrUtils.isEmpty(consumeQueryDto.getUserUUID())) {
            return Mutual.invalidParamAppend("userUUID");
        }
        if(StrUtils.isEmpty(consumeQueryDto.getId())) {
            return Mutual.invalidParamAppend("id");
        }
        return payService.consumeQuery(consumeQueryDto);
    }

    @RequestMapping(value = "/consumeQueryUni")
    public Reply consumeQueryUni(@RequestBody ConsumeQueryDto consumeQueryDto){
        if(StrUtils.isEmpty(consumeQueryDto.getUserUUID())) {
            return Mutual.invalidParamAppend("userUUID");
        }
        if(StrUtils.isEmpty(consumeQueryDto.getId())) {
            return Mutual.invalidParamAppend("id");
        }
        return payService.consumeQueryUni(consumeQueryDto);
    }

    @RequestMapping(value = "/backRcvResponse")
    public Object callBackBackRcvResponse(HttpServletRequest request, HttpServletResponse response) throws Exception {
        payService.backRcvResponse(request, response);
        return null;
    }


    /**
     * -----------------------只用于测试
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/frontRcvResponse")
    @ResponseBody
    public Object callBackFrontRcvResponse(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return payService.frontRcvResponse(request, response);
    }

    @RequestMapping(value = "/testPostData")
    @ResponseBody
    public String testPostData(@RequestParam String transactionRecordId, @RequestParam String postUrl){
        //
        return payService.testPostData(transactionRecordId, postUrl);
    }


    @RequestMapping(value = "/testPostBack")
    @ResponseBody
    public String testPostBack(@RequestBody String postStr){
        LogUtil.writeLog("回调接收到的参数：" + postStr);
        return "SUCCESS";
    }


}
